package com.gamejelly.proxy.server;

import com.gamejelly.proxy.handler.ReqCodec;
import com.gamejelly.proxy.handler.RespCodec;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import lombok.Setter;
import org.springframework.util.Assert;

/**
 * Created by haoyifen on 2017/6/19 18:52.
 */
public class ServerChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Setter
    private RpcServerHandler rpcServerHandler;
    private final LengthFieldPrepender lengthFieldPrepender = new LengthFieldPrepender(4, 0);

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        Assert.notNull(rpcServerHandler, "rpc handler is null");
        ch.pipeline().addLast(
                new LengthFieldBasedFrameDecoder(1024 * 1024 * 1024, 0, 4, 0, 4),
                new ReqCodec(),
                lengthFieldPrepender,
                new RespCodec(),
                rpcServerHandler);
    }
}
